package com.demo.task.ext

import android.util.Log
import com.demo.task.bean.PlanBean
import com.orhanobut.logger.Logger
import java.io.File

/**
 * 获取全部文件地址
 */
fun List<String>.filterLocalUrls(): List<String> {
    val file = File(getResPath())
    val listFiles = file.listFiles()?.map { it.name } ?: emptyList()
    return this.filter { !listFiles.contains(it) }
}

fun PlanBean.getFileUrls(): List<String> {
    val list = mutableListOf<String>()
    plan?.forEach {
        it.group.forEach {
            it.pages.forEach {
                it.box.forEach {
                    it.list.forEach {
                        if (it.type in 1..2) {
                            list.add(it.content)
                        }
                    }
                }
            }
        }
    }
    return list.distinct().also {
        Log.i("DataExt", "getFileUrls: ${it.size} $it")
        Logger.i("getFileUrls: ${it.size} $it")
    }
}

/**
 * 判断box是否都一样
 */
fun List<PlanBean.Plan.Group.Page.Box>?.isSameData(): Boolean {
    if (isNullOrEmpty()) return false

    // 检查所有 Box.list 的大小相同
    val firstSize = first().list.size
    if (any { it.list.size != firstSize }) return false

    // 检查每个索引 i 的 content 都相同
    return (0 until firstSize).all { i ->
        map { it.list[i].content }.distinct().size == 1
    }
}